package lrscp.lib.io.Monitor;

import java.io.File;

import lrscp.lib.Handler;
import lrscp.lib.Log;

public class FileMonitor {
    public String curFileName = null;
    public int leftFileCount = 0;
    public int filePercent = 0;

    private void updateProgressDisplay() {
        Handler.remove(disp);
        Handler.post(disp);
    }

    private SimpleMonitor dirMonitor = new SimpleMonitor() {
        public void updateProgress(long progress) {
            leftFileCount = (int) (mSize - progress);
            updateProgressDisplay();
        };

        public void updateTarget(Object target) {
            curFileName = ((File) target).getName();
            updateProgressDisplay();
        };
    };

    private SimpleMonitor fileMonitor = new SimpleMonitor() {
        public void updatePercent(int percent) {
            filePercent = percent;
            updateProgressDisplay();
        };
    };

    public final Runnable DEFAULT_DISP = new Runnable() {
        @Override
        public void run() {
            Log.print(String.format("\r%1$d files left, %%%2$d finished, current file: %3$s",
                    leftFileCount, filePercent, curFileName));
        }
    };

    private Runnable disp = DEFAULT_DISP;

    public ProgressMonitor getDirectoryMonitor() {
        return dirMonitor.getMonitor();
    }

    public ProgressMonitor getFileMonitor() {
        return fileMonitor.getMonitor();
    }

    public void setDisplay(Runnable runnable) {
        Handler.remove(disp);
        disp = runnable;
        Handler.post(disp);
    }
}
